<?php 
   require_once '../funciones.php';
//    session_start();
    $soporte=$_SESSION['caweb']['soporte'];
    $comercial=$_SESSION['caweb']['comercial'];
    $sucursal = $_SESSION['caweb']['sucursal'];
//    $base=$_SESSION['caweb']['basededatos'];
//
//    $dbhost="localhost";
//    $dbuser="caweb";
//    $dbpassword="caweb";
//    $database=$base;
    $con = con_caweb();
    $transaccion = $_REQUEST['transaccion'];
//    $nit        = $_REQUEST['nit'];
	$page = $_REQUEST['page'];

	// get how many rows we want to have into the grid - rowNum parameter in the grid
	$limit = $_REQUEST['rows'];

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel
	$sidx = $_REQUEST['sidx'];

	// sorting order - at first time sortorder
	$sord = $_REQUEST['sord'];

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;

        $sql = "select count(transaccion) as count from temporal_aux where transaccion = $transaccion";
        $result = mysql_query($sql, $con);
	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	$count = $row['count'];

	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
        $sql = "select * from temporal_aux where transaccion = $transaccion  ORDER BY $sidx $sord LIMIT $start , $limit";
	$result = mysql_query( $sql ) or die("Error SQL1.".mysql_error());

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";
//Contenido de los campos en la tabla temporal_aux
//varchar1  ->  Ajuste
//varchar2  ->  Observacion
//varchar3  ->  cliente
//varchar4  ->  Estado Cartera
//entero1   ->  Facturasistema
//decimal1  ->  Retefuente
//decimal2  ->  ReteIva
//decimal3  ->  ReteIca
//decimal4  ->  Otros
//decimal5  ->  Cuadre
	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		echo "<row id='". $row[temId]."'>";
		echo "<cell>". $row[varchar1]."</cell>";
                //Con la informacion de la facturasistema se obtiene la informacion de la factura
                $sql = "select factura,fechafactura,tipo,base,iva,total,estado,juridico,fechapago,valorpagado from cabeceras_fac where facturasistema = {$row['entero1']}";
                $tmpResult = mysql_query($sql,$con) or die("Error SQL 3".mysql_error().$sql);
		echo "<cell>". mysql_result($tmpResult, 0,'factura')."</cell>";   //Factura
                echo "<cell>". mysql_result($tmpResult, 0,'fechafactura')."</cell>";   //Fechafactura
                echo "<cell>". mysql_result($tmpResult, 0,'tipo')."</cell>";   //Tipo
                echo "<cell>". mysql_result($tmpResult, 0,'base')."</cell>";   //Base
                echo "<cell>". mysql_result($tmpResult, 0,'iva')."</cell>";   //IVA
                echo "<cell>". mysql_result($tmpResult, 0,'total')."</cell>";   //Total
                echo "<cell>". mysql_result($tmpResult, 0,'estado')."</cell>";   //Estado Factura
                echo "<cell>". $row[varchar4]."</cell>";
                echo "<cell>". mysql_result($tmpResult, 0,'juridico')."</cell>";   //Juridico
                echo "<cell>". mysql_result($tmpResult, 0,'fechapago')."</cell>";   //Fecha Pago
                echo "<cell>". mysql_result($tmpResult, 0,'valorpagado')."</cell>";   //Valor pagado
		echo "<cell>". $row[decimal1]."</cell>";
                echo "<cell>". $row[decimal2]."</cell>";
                echo "<cell>". $row[decimal3]."</cell>";
		echo "<cell>". $row[decimal4]."</cell>";
                echo "<cell>". $row[decimal5]."</cell>";
                echo "<cell>". $row[varchar2]."</cell>";
		echo "</row>";
	}
	echo "</rows>";
	 
?>
